package BufferProcessTest;

import org.junit.Test;

import java.io.*;

/**
 * @author 李泽伟
 */
public class MainTest {
    @Test
    public void test() {
        //使用处理流实现文件复制
        //1.造文件
        File srcFile = new File("test.png");
        File desFile = new File("testCopyStream.png");
        //2.造流
        //2.1造节点流
        FileOutputStream desStream = null;
        FileInputStream srcStream = null;
        BufferedInputStream srcBufStream = null;
        BufferedOutputStream desBufStream = null;
        try {
            desStream = new FileOutputStream(desFile);
            srcStream = new FileInputStream(srcFile);
            //2.2造处理流
            srcBufStream = new BufferedInputStream(srcStream);
            desBufStream = new BufferedOutputStream(desStream);
            //3.操作细节，读取与写入
            byte[] buf = new byte[10];
            int len;
            while ((len = srcBufStream.read(buf))!=-1){
                desBufStream.write(buf, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //4.关闭流（只需要关闭外层的处理流,内部自动关闭节点流）
            try {
                if (srcBufStream!=null) {
                    srcBufStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (desBufStream!=null) {
                    desBufStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        //对比使用处理流的速度
        long startBuf = System.currentTimeMillis();
        copyBufferedFile("test.mp4", "testBuffered.mp4");
        long endBuf = System.currentTimeMillis();
        //66ms
        System.out.println(endBuf - startBuf);

        long start2 = System.currentTimeMillis();
        copyNotBufferedFile("test.mp4", "testNotBuffered.mp4");
        long end2 = System.currentTimeMillis();
        //237ms
        System.out.println(end2 - start2);
    }

    public static void copyBufferedFile(String srcPath, String desPath){
        //1.造文件
        File srcFile = new File(srcPath);
        File desFile = new File(desPath);
        //2.造流
        //2.1造节点流
        BufferedInputStream srcBufStream = null;
        BufferedOutputStream desBufStream = null;
        try {
            FileOutputStream desStream = new FileOutputStream(desFile);
            FileInputStream srcStream = new FileInputStream(srcFile);
            //2.2造处理流
            srcBufStream = new BufferedInputStream(srcStream);
            desBufStream = new BufferedOutputStream(desStream);
            //3.操作细节，读取与写入
            byte[] buf = new byte[1024];
            int len;
            while ((len = srcBufStream.read(buf))!=-1){
                desBufStream.write(buf, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            //4.关闭流（只需要关闭外层的处理流,内部自动关闭节点流）
            try {
                if (srcBufStream!=null) {
                    srcBufStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (desBufStream!=null) {
                    desBufStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void copyNotBufferedFile(String srcPath, String desPath){
        FileInputStream srcStream = null;
        FileOutputStream desStream = null;
        File srcFile = new File(srcPath);
        File desFile = new File(desPath);
        try {
            srcStream = new FileInputStream(srcFile);
            desStream = new FileOutputStream(desFile);
            byte[] bytes = new byte[1024];
            int len;
            while ((len = srcStream.read(bytes))!=-1){
                desStream.write(bytes, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (srcStream != null) {
                    srcStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (desStream != null) {
                    desStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test1(){
        BufferedReader srcReader = null;
        BufferedWriter srcWriter = null;
        try {
            srcWriter = new BufferedWriter(new FileWriter(new File("outHello.txt")));
            srcReader = new BufferedReader(new FileReader(new File("hello.txt")));
            //读取方式1
            char[] buf = new char[1024];
            int len;
            while ((len = srcReader.read(buf)) != -1) {
                srcWriter.write(buf, 0, len);
            }
            //读取方式2
            String sBuf;
            while ((sBuf = srcReader.readLine())!=null){
                // 方法1
                srcWriter.write(sBuf+"\n");
                //方法2
                srcWriter.write(sBuf);
                srcWriter.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (srcReader != null) {
                    srcReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (srcWriter != null) {
                    srcWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
